<?php

namespace App\Http\Controllers\Mobile;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class MIndexController extends Controller
{
    //首页
    public function index(Request $request){
        //判断用户是否登录
        $session_data = $request->session()->get('eai');
        $typename = 'FXXAUUSD,FXEURUSD,FXGBPUSD,FXUSDJPY,FXUSDCHF';
        if($session_data){
            //判断用户是否有品种信息
            $isExist = DB::table('waihui_member_typename')->where(array('userid'=>$session_data['id']))->first();
            if($isExist){
                //获取用户品种信息
                $typename = $isExist->typename;
            }else{
                //给用户添加默认品种
                DB::table('waihui_member_typename')->insertGetId(array('userid'=>$session_data['id'],'typename'=>$typename));
            }
        }
        //开盘停盘时间
        $starttime  = strtotime(date("Y-m-d H:i:s",mktime(5,0,0,date("m"),date("d")-date("w")+(date("w")==0?-6:1),date("Y")))); //周一早上5:00:00
        $endtime    = strtotime(date("Y-m-d H:i:s",mktime(5,0,0,date("m"),date("d")-date("w")+(date("w")==0?-1:6),date("Y")))); //周六早上5:00:00
//        return view('mobile.index',compact('typename','starttime','endtime'));
        return view('newmobile.index',compact('typename','starttime','endtime'));
    }
    //登录
    public function mLogin(){
            return view('mobile.login');
    }
    //注册
    public function mRegister(){
            return view('mobile.register');
    }
    //添加
    public function mAppend(Request $request){
        //判断用户是否登录
        $session_data = $request->session()->get('eai');
        if(!$session_data){
            return redirect('mlogin');
        }
        //平台总的品种名称
        $typenamelist = array(0 => "FXXAUUSD",1 => "FXEURUSD",2 => "FXGBPUSD",3 => "FXUSDJPY",4 => "FXUSDCHF",5 => "FXUSDCAD",6 => "FXAUDUSD",7 => "FXEURGBP",8 => "FXEURJPY",9 => "FXEURAUD",10 => "FXEURCHF",11 => "FXEURCAD",12 => "FXGBPCHF",13 => "FXGBPJPY",14 => "FXCADJPY",15 => "FXAUDCAD",16 => "FXCHFJPY",17 => "FXAUDJPY");
        //用户已添加的品种名称
        $typestr   = DB::table('waihui_member_typename')->where(array('userid'=>$session_data['id']))->value('typename');
        //差集获取用户没有添加的品种名称
        $diffarr   = array_diff($typenamelist,explode(',',$typestr));
        $diffstr   = str_replace('FX','',implode(',',$diffarr));
        //获取品种信息
        $typearr   = DB::table('waihui_type')->select('*')->whereIn('name',explode(',',$diffstr))->orderBy('name','asc')->get()->toArray();
        //按照品种首字母分组
        $typenamearr = [];
        foreach ($typearr as $key=>$val){
            $first = substr($val->name,0,1);
            $typenamearr[$first][] = $val;
        }
        $firststr = implode(',',array_keys($typenamearr));
        return view('mobile.append',compact('diffstr','typenamearr','firststr'));
    }
    //用户删除品种typename
    public function delTypename(Request $request){
        //判断用户是否登录
        $session_data = $request->session()->get('eai');
        if(!$session_data){
            return json_encode(array('status'=>0,'msg'=>'用户未登录'));
        }
        //获取品种名称
        $typename = trim($request->typename);
        if($typename == ''){
            return json_encode(array('status'=>1,'msg'=>'信息不正确'));
        }
        //删除该品种
        $typstr   = DB::table('waihui_member_typename')->where(array('userid'=>$session_data['id']))->value('typename');
        $typearr  = explode(',',$typstr);
        unset($typearr[array_search($typename,$typearr)]);
        //更新品种信息
        $result   = DB::table('waihui_member_typename')->where(array('userid'=>$session_data['id']))->update(array('typename'=>implode(',',$typearr)));
        if($result){
            return json_encode(array('status'=>2,'msg'=>'删除成功'));
        }else{
            return json_encode(array('status'=>3,'msg'=>'删除失败'));
        }
    }
    //用户添加品种typename
    public function addTypename(Request $request){
        //判断用户是否登录
        $session_data = $request->session()->get('eai');
        if(!$session_data){
            return json_encode(array('status'=>0,'msg'=>'用户未登录'));
        }
        //获取品种名称
        $typename = trim($request->typename);
        if($typename == ''){
            return json_encode(array('status'=>1,'msg'=>'信息不正确'));
        }
        //获取用户品种信息
        $typstr   = DB::table('waihui_member_typename')->where(array('userid'=>$session_data['id']))->value('typename');
        $typearr  = explode(',',$typstr);
        if(in_array($typename,$typearr)){
            return json_encode(array('status'=>2,'msg'=>'品种已存在'));
        }
        $typstr   = $typstr? $typename.','.$typstr : $typename;
        //更新用户品种信息
        $result   = DB::table('waihui_member_typename')->where(array('userid'=>$session_data['id']))->update(array('typename'=>$typstr));
        if($result){
            return json_encode(array('status'=>3,'msg'=>'添加成功'));
        }else{
            return json_encode(array('status'=>4,'msg'=>'添加失败'));
        }
    }
    //协议
    public function mAgree(){
        return view('mobile.agree');
    }
    //忘记密码
    public function forgetpswd(){
        return view('mobile.forget');
    }
    //赛事规则
    public function tournament(){
        return view('mobile.tournament');
    }
    //策略协议
    public function callorder(){
        return view('mobile.callorder');
    }
    //奖项设置
    public function competition(){
        return view('mobile.competition');
    }
    //周榜
    public function weekrank(Request $request){
    	$start = trim($request->start);
    	$end   = trim($request->end);
        return view('mobile.weekrank',compact('start','end'));
    }
    //活动页比赛排名详情
    public function rankDetail(Request $request){
    	$uid = intval(trim($request->userid));
    	$memberinfo = memberInfo($uid);
    	return view('mobile.rankdetail',compact('memberinfo'));
    }
}
